<?php
namespace Admin\Controller;
use Lib\Api\RebateRatioApi;
use Think\Controller;
class RebateRatioController extends Controller {

    public function index(){
        $this->display();
    }

    public function tolist() {
        $field = 'b.id, b.show_name, group_concat(concat("第",rebate_level,"经纪人"),"：",concat("返佣",rebate_ratio,"%") order by  rebate_level SEPARATOR "，") as rebate_ratio';
        $left = 'left join fs_game_type b on b.id = a.game_type';
        $rebateRatioList = RebateRatioApi::getList(array(), $left, $field, "a.game_type");

        if (!empty($rebateRatioList)) {
            for ($i=0; $i<count($rebateRatioList); $i++) {
                $rebateRatioList[$i]["rebate_ratio"] = str_replace("第1经纪人","第一经纪人",$rebateRatioList[$i]["rebate_ratio"]);
                $rebateRatioList[$i]["rebate_ratio"] = str_replace("第2经纪人","第二经纪人",$rebateRatioList[$i]["rebate_ratio"]);
            }
        }

        $rel['data'] = $rebateRatioList;
        $this->ajaxReturn($rel);
    }

    public function get_data() {
        $play_type = $_POST['play_type'];
        $rebateRatioList = RebateRatioApi::getList(array("game_type"=>$play_type), "", "*", "", "rebate_level");

        if (!empty($rebateRatioList)) {
            for ($i=0; $i<count($rebateRatioList); $i++) {
                $rebateRatioList[$i]["rebate_level"] = str_replace("1","一",$rebateRatioList[$i]["rebate_level"]);
                $rebateRatioList[$i]["rebate_level"] = str_replace("2","二",$rebateRatioList[$i]["rebate_level"]);
            }
        }

        $rel['data'] = $rebateRatioList;
        $this->ajaxReturn($rel);
    }

    public function addoreditbigtradeconfig() {
        $play_type = $_POST['play_type'];
        $rebate_ratio_arr = $_POST['rebate_ratio'];
        RebateRatioApi::delete(array("game_type"=>$play_type));

        $rebate_ratio_list = array();
        for ($i=0; $i<count($rebate_ratio_arr); $i++) {
            if (!empty($rebate_ratio_arr)) {
                $rebate_ratio["game_type"] = $play_type;
                $rebate_ratio["rebate_level"] = ($i+1);
                $rebate_ratio["rebate_ratio"] = $rebate_ratio_arr[$i];
                array_push($rebate_ratio_list, $rebate_ratio);
            }
        }
        if (count($rebate_ratio_list) > 0) {
            RebateRatioApi::addAll($rebate_ratio_list);
        }

        $rel['code'] = 0;
        $rel['msg'] = "返佣配置已设定";
        $this->ajaxReturn($rel);

    }
}